package com.woniuxy.lifeorder.service.controller;


import com.woniuxy.life.core.api.CommonResult;
import com.woniuxy.lifeorder.service.entity.Orderitem;
import com.woniuxy.lifeorder.service.service.OrderitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单项表 前端控制器
 * </p>
 *
 * @author 陈瑜洲，吴继明
 * @since 2020-09-24
 */
@RestController
@RequestMapping("/orderitem")
public class OrderitemController {
    @Autowired
    private OrderitemService orderitemService;
    @GetMapping("/list")
    public CommonResult<List<Orderitem>> listCommonResult(Orderitem orderitem){
        CommonResult<List<Orderitem>> listCommonResult=new CommonResult<>();
        try {
                List<Orderitem> orderitems=orderitemService.orderitem_query(orderitem);
                listCommonResult.setData(orderitems);
                listCommonResult.setMessage("订单项查询成功");
                listCommonResult.setCode(200L);
        }catch (Exception e){
            listCommonResult.setCode(500L);
            listCommonResult.setMessage("订单查询失败");
        }
        return listCommonResult;
    }
    @PostMapping("/insertone")
    public CommonResult insertOne(Orderitem orderitem) {
        if (orderitem != null) {
            try {
                orderitemService.save(orderitem);
                return CommonResult.success();
            } catch (Exception e) {
                return CommonResult.failed(500L, "订单项生成失败");
            }
        } else {
                return CommonResult.failed(500L,"参数错误");
        }

    }
    @PostMapping("/insertAll")
    public CommonResult insertAll(List<Orderitem> orderitems){
        if (orderitems!=null){
            try {
            orderitemService.saveBatch(orderitems);
                return CommonResult.success();
            }catch (Exception e){
                return CommonResult.failed(500L,"订单项生成异常");
        }
        }else {
            return CommonResult.failed(500L,"参数错误");
        }
    }
    @DeleteMapping("/delete")
    public CommonResult deleteOne(Integer orderitemId){
        if (orderitemId!=null){try {
            orderitemService.removeById(orderitemId);
            return CommonResult.success();
        }catch (Exception e){
            return CommonResult.failed(500L,"订单项删除失败");
        }}else {
            return CommonResult.failed(500L,"参数异常");
        }
    }
}
